package com.ds.dsm.manager.repository;

import com.ds.common.JDSException;
import com.ds.common.database.metadata.ProviderConfig;
import com.ds.common.database.metadata.TableInfo;
import com.ds.dsm.repository.db.service.DSMTableService;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.annotation.TreeAnnotation;
import com.ds.esd.custom.annotation.nav.TabsAnnotation;
import com.ds.esd.custom.annotation.toolbar.BottomBarMenu;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.tool.ui.component.list.TreeListItem;
import com.ds.esd.tool.ui.enums.SelModeType;

import java.util.HashMap;
import java.util.List;

@TabsAnnotation(singleOpen = true)
@BottomBarMenu()
@TreeAnnotation(heplBar = true, selMode = SelModeType.multibycheckbox, customService = DSMTableService.class, bottombarMenu = {CustomFormMenu.Save, CustomFormMenu.Close})
public class SelectTableTopTree extends TreeListItem {


    public SelectTableTopTree(String projectVersionName) {
        this.caption = "数据库";
        this.setEuClassName("db.provider.ProviderGallery");
        this.setIniFold(false);
        this.addTagVar("projectVersionName", projectVersionName);
        this.setImageClass("spafont spa-icon-coin");
        this.id = "all";
        try {
            List<ProviderConfig> configs = ESDFacrory.getESDClient().getAllDbConfig();
            if (configs.size() > 0) {
                for (ProviderConfig child : configs) {
                    this.addChild(new SelectTableTopTree(child, projectVersionName));
                }
            }
        } catch (JDSException e) {
            e.printStackTrace();
        }
    }

    public SelectTableTopTree(TableInfo tableInfo, String projectVersionName) {
        this.caption = tableInfo.getName() + "(" + tableInfo.getCnname() + ")";
        this.setEuClassName("dsm.manager.table.Cols");
        this.setImageClass("spafont spa-icon-c-grid");
        this.setIniFold(false);
        this.tagVar = new HashMap<>();
        this.tagVar.put("tablename", tableInfo.getName());
        this.tagVar.put("configKey", tableInfo.getConfigKey());
        this.tagVar.put("projectVersionName", projectVersionName);
        this.id = tableInfo.getName();

    }

    public SelectTableTopTree(ProviderConfig config, String projectVersionName) {
        this.setImageClass("iconfont iconchucun");
        this.caption = config.getConfigName() == null ? config.getConfigKey() : config.getConfigName();
        this.setEuClassName("dsm.manager.table.TableList");
        this.setIniFold(false);
        this.tagVar = new HashMap<>();
        this.tagVar.put("configKey", config.getConfigKey());
        this.tagVar.put("projectVersionName", projectVersionName);
        this.id = config.getConfigKey() == null ? config.getConfigName() : config.getConfigKey();
        try {
            List<TableInfo> tableInfos = ESDFacrory.getESDClient().getDbFactory(config.getConfigKey()).getTableInfos(null);
            if (tableInfos.size() > 0) {
                for (TableInfo child : tableInfos) {
                    this.addChild(new SelectTableTopTree(child, projectVersionName));

                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

    }

}
